home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 5633 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: dtek.chalmers.se!d1mag
  2. From: d1mag@dtek.chalmers.se (Christian Magnusson)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: My code crashes my computer
  5. Date: 18 Mar 1996 00:00:17 GMT
  6. Organization: Chalmers University of Technology
  7. Message-ID: <4ii92h$ktm@nyheter.chalmers.se>
  8. References: <4igm25$poj@goofy.BrandonU.CA>
  9. NNTP-Posting-Host: gleen.dtek.chalmers.se
  10.  
  11. In article <4igm25$poj@goofy.BrandonU.CA>,  <menon@beijing.brandonu.ca> wrote:
  12. >This is an except from my program which shows where it crashes
  13. >
  14.  
  15.  
  16. >/***** READ_FILE *****/
  17. >void read_file(char *infile, char *infile2){
  18. >   int i=0, x, y, garb;
  19. >   FILE *fp;
  20. >   char *s="File Error";                <--   char s[80] = "File Error"; 
  21. >   char *comment="", buf;               <--   char comment[80];
  22.                                          <--   char buf[80];
  23. >
  24. >   fp = fopen(infile, "r");
  25. >   if (fp==NULL){
  26. >         printf("FAILED!\n");
  27. >      perror(s);
  28. >         exit(1);
  29. >         }
  30. >   fgets(type,3,fp);
  31. >   fscanf(fp,"%s",comment);
  32. >   if (comment[0]=='#'){
  33. >     do{
  34. >        fscanf(fp, "%s",comment); 
  35. >          sprintf(buf,"%d",atoi(comment));  <---Crashes Here
  36. >          }
  37. >     while (strcmp(comment,buf)); 
  38.  
  39.  
  40. Hey... Try to learn how to use the char pointers!  You are lucky that you 
  41. could compile it at all.
  42.  
  43. buf was assigned as a single character, not char buffer!  sprintf must be
  44. used with a char pointer as it's first argument.
  45.  
  46. comment was only defined as a char pointer. You didn't allocate any
  47. memory for that string!
  48.  
  49. >
  50. >If anyone can help me, I would greatly appreciate it.
  51. >
  52.  
  53. I hope that helped you a bit.
  54.  
  55. /Mag
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. --
  70. |  Christian 'Mag' Magnusson        Computer Science and Engineering
  71. |  Email:  d1mag@dtek.chalmers.se    Chalmers University of Technology
  72. |  GSM:    +46 (0)705 380580        WWW: http://www.dtek.chalmers.se/~d1mag
  73. |  http://www.dtek.chalmers.se/~d1mag/GPL.html (Gothenburg Pinball League)
  74.